public class CountLettersInArray {

	// Beginning of method main

	public static void main(String[] args) {

		// Declare and create an array

		char[] chars = createArray();

		// Display the array

		System.out.println("The lowercase letters are:");

		displayArray(chars);

		// Count the occurrences of each letter

		int[] counts = countLetters(chars);

		// Display counts

		System.out.println();

		System.out.println("The occurrences of each letter are:");

		displayCounts(counts);

	} // End of main

	/* Create an array of characters */

	// Beginning of char[] createArray()

	public static char[] createArray() {

		// Declare an array of characters and create it

		char[] chars = new char[100];

		// Create lowercase letters randomly and assign them to the array

		for (int i = 0; i < chars.length; i++)

			chars[i] = readChar();

		// Return the array

		return chars;

	} // End of char[] createArray()
	/* Display the array of characters */

	// Beginning of void displayArray(char[])
	public static void displayArray(char[] chars) {

		// Display the characters in the array 20 on each line
		for (int i = 0; i < chars.length; i++) {
			if ((i + 1) % 20 == 0)

				System.out.println(chars[i] + " ");

			else

				System.out.print(chars[i] + " ");

		}

	} // End of void displayArray(char[])
	/* Count the occurrences of each letter */

	// Beginning of int[] countLetters(char[])
	public static int[] countLetters(char[] chars) {
		// Declare and create an array of 26 int
		int[] counts = new int[26];
		// For each lowercase letter in the array, count it
		for (int i = 0; i < chars.length; i++)

			counts[chars[i] - 'a']++;

		return counts;
	} // End of int[] countLetters(char[])
	/* Display counts */

	// Beginning of void displayCounts(int[])

	public static void displayCounts(int[] counts) {

		for (int i = 0; i < counts.length; i++) {

			if ((i + 1) % 10 == 0)

				System.out.println(counts[i] + " " + (char) (i + 'a'));

			else

				System.out.print(counts[i] + " " + (char) (i + 'a') + " ");

		}

	} // End of void displayCounts(int[])

	public static char rndChar() {
		int rnd = (int) (Math.random() * 52);
		char base = (rnd < 26) ? 'A' : 'a';

		return (char) (base + rnd % 26);
	}

} // End of class CountLettersInArray